/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.report;

import entity.ServiceRequest;
import entity.Sample;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import java.util.*;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
/**
 *
 * @author JAVIER
 */
public class letterLeftOver implements JRDataSource {
    
    public letterLeftOver(ServiceRequest ser){
        service = ser;
    }

    private ServiceRequest service = new ServiceRequest();
    private int sampleactually = -1;


    public Object getFieldValue(JRField jrf) throws JRException
    {
        Object valor = null;
        Sample sample = null;

        if ("client".equals(jrf.getName()))
        {
            valor = service.getClient().getName();
        }
        else if ("totalSamples".equals(jrf.getName()))
        {
            valor = service.getTotalSamples();
        }
        else if ("registrationDate".equals(jrf.getName()))
        {
            valor = "fecha registracion";
            DateTimeFormatter formatter = DateTimeFormat.forPattern("dd MMMM yyyy");
            valor = formatter.print(service.getRegistrationDate());
        }
        else if ("date".equals(jrf.getName()))
        {
            valor = "12 de Diciembre de 2008";//service.get(sampleactually).getComentarios();
        }      
        else if ("sample".equals(jrf.getName()))
        {
            sample = (Sample) service.getSamples().get(sampleactually);
            valor = sample.getName();
        }
        else if ("quantity".equals(jrf.getName()))
        {
            sample = (Sample)service.getSamples().get(sampleactually);
            valor = sample.getQuantity();
        }
        else if ("responsible".equals(jrf.getName()))
        {
            valor = service.getResponsible();
        }

        return valor;
    }

    public boolean next() throws JRException
    {
        return ++sampleactually < service.getSamples().size();
    }
    
    public void Print() throws JRException{
        
        letterLeftOver datasource = this;
        
        JasperReport reporte = (JasperReport) JRLoader.loadObject("CartaAlpha1.jasper");  
        JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null,datasource );  

        JRExporter exporter = new JRPdfExporter();  
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("report2PDF.pdf")); 
        exporter.exportReport(); 
    
    }
    
}
